from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import *

from instruments.sa import RSSignalAnalyzer, KeysightSignalAnalyzer
from instruments.sg import KeysightSignalGenerator, RSSignalGenerator
from threads import ConnectThread


class InstrumentInfoWidget(QWidget):
    """
        设备编辑控件：
        包含：connect按钮，IP地址输入框等
    """
    scpi_emitted = pyqtSignal(str)

    def __init__(self, instrument_type_name: str, instrument_model_list, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.instrument_model_list = instrument_model_list  # 仪器仪表型号
        self.instrument_type_name = instrument_type_name  # 仪器仪表类型
        self.instrument_type = QLabel(f"{instrument_type_name}")
        self.instrument_model_combobox = QComboBox()
        # VISA Address
        self.vias_address_editer = QLineEdit()
        self.vias_address_editer.setMinimumWidth(100)
        self.port_combox = QComboBox()
        self.port_combox.addItems([str(i + 1) for i in range(8)])
        self.port_label = QLabel('Port:')
        self.h_layout = QHBoxLayout()
        self.h_layout.addWidget(self.vias_address_editer)
        self.h_layout.addWidget(self.port_label)
        self.h_layout.addWidget(self.port_combox)
        self.instrument_model_combobox.addItems(self.instrument_model_list)
        self.button_connection = QPushButton("Connect")
        self.main_layout = QHBoxLayout()
        self.main_layout.addWidget(self.instrument_type)
        self.main_layout.addWidget(self.instrument_model_combobox)
        self.main_layout.addLayout(self.h_layout)
        self.main_layout.addWidget(self.button_connection)

        self.setLayout(self.main_layout)

        self.main_layout.setStretch(0, 2)
        self.main_layout.setStretch(1, 2)
        self.main_layout.setStretch(2, 7)
        self.main_layout.setStretch(3, 1)

        self.instrument = None
        self.connect_thread = ConnectThread()

        # 信号与槽
        self.connect_thread.connect_state_signal.connect(self.connect_finished)
        self.button_connection.clicked.connect(self.connect)

        # 其他
        if instrument_type_name != 'Switch Box':
            self.port_combox.hide()
            self.port_label.hide()


        # 信号与槽

    def connect(self):
        # 根据设备型号选择程序中对应的类
        if self.instrument:
            if self.instrument_type_name == 'Switch Box':
                # 尝试解决开关箱连不上的问题
                del self.instrument
        self.instrument = self.get_instrument()
        if self.instrument_type_name != 'Switch Box' :
            self.instrument.scpi_emitted.connect(self.emit_scpi)
        self.button_connection.setStyleSheet('background-color: rgb(255, 255, 255);')
        # 不卡GUI
        self.connect_thread.instrument = self.instrument
        self.connect_thread.start()
        # self.connect_thread.connect_state_signal.connect(self.connect_finished)

        self.button_connection.setStyleSheet('background-color: rgb(248, 223, 112);')
        self.button_connection.setText("Connecting")

    def connect_finished(self, state: bool):
        if state:
            self.button_connection.setStyleSheet('background-color: rgb(0, 255, 0);')
            self.button_connection.setText("Connection succeeded")
        else:
            instrument_model = self.instrument_model_combobox.currentText()
            QMessageBox.about(self, 'Info', f'{instrument_model}:Instrument connection failed!')
            self.button_connection.setText("Connection failed")
            self.button_connection.setStyleSheet('background-color: rgb(255, 0, 0);')

    def get_instrument(self):
        instrument_model = self.instrument_model_combobox.currentText()
        instrument_address = self.vias_address_editer.text()
        if self.instrument_type_name == 'SA':
            if instrument_model == 'Keysight':
                instrument = KeysightSignalAnalyzer(instrument_address, self)
            else:
                instrument = RSSignalAnalyzer(instrument_address, self)
            return instrument
        elif 'SG' in self.instrument_type_name:
            if instrument_model == 'Keysight':
                instrument = KeysightSignalGenerator(instrument_address, self)
            else:
                instrument = RSSignalGenerator(instrument_address, self)
            return instrument
        else:
            return None

    def emit_scpi(self, scpi_info: str):
        self.scpi_emitted.emit(scpi_info)
